<?php

/**
 * uniBoard PHPMailer Wrapper
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-15 17:35:38 +0000 (Wed, 15 Sep 2010) $
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class class_mailer{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore				= NULL;
	
	/**
	 * Mass Mailing?
	 *
	 * @var false
	 */
	
	private $mass_mailing			= false;
	
	/**
	 * PHP-Mailer instance
	 *
	 * @var object
	 */
	
	private $php_mailer				= NULL;
	
	/**
	 * Number of mails to send
	 *
	 * @var integer
	 */
	
	private $sent_emails			= 0;
	
	/**
	 * Sending Queue
	 *
	 * @var string
	 */
	
	private $queue					= '';
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore, $mass_mailing = false){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
	
		// Bring PHP-Mailer to life
		$this -> php_mailer = new class_phpmailer();

		// Mass Mailing?
		$this -> mass_mailing = $mass_mailing;
		
		// Keep Alive
		$this -> php_mailer -> SMTPKeepAlive = $mass_mailing;
		
		// Are we using SMTP?
		if ( $appCore -> conf['board_email_use_smtp'])
		{
			// Use SMTP
			$this -> php_mailer -> IsSMTP();
						
			// Use Auth
			$this -> php_mailer -> SMTPAuth 	= true;
			
			// Host and Port
			$this -> php_mailer -> Host 		= $appCore -> conf['board_email_smtp_host'];
			$this -> php_mailer -> Port 		= $appCore -> conf['board_email_smtp_port'];
			
			// User Name and Password
			$this -> php_mailer -> Username 	= $appCore -> conf['board_email_smtp_user_name'];
			$this -> php_mailer -> Password 	= $appCore -> conf['board_email_smtp_user_pass'];
		}
		else
		{
			// Tell Mailer to use mail()
			$this -> php_mailer -> IsMail();
		}
		
		// Tell Mailer to send ONE MAIL AT TIME
		$this -> php_mailer -> SingleTo = true;
		
		// Set Board data
		$this -> php_mailer -> SetFrom( $appCore -> conf['board_outcoming_email'], $appCore -> conf['board_name'] . ( isset($appCore -> conf['board_description'][0]) ? ' - ' . $appCore -> conf['board_description'] : ''));
		$this -> php_mailer -> AddReplyTo( $appCore -> conf['board_incoming_email'], $appCore -> conf['board_name'] . ( isset($appCore -> conf['board_description'][0]) ? ' - ' . $appCore -> conf['board_description'] : ''));
				
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Send email
	 *
	 * @param string $address
	 * @param string $recipient
	 * @param string $subject
	 * @param string $message
	 */
	
	function sendEmail( $address, $recipient, $subject, $message){
		
		// Are we Queing emails?
		if ( $this -> sent_emails >= $this -> appCore -> conf['board_email_package_size'])
		{
			// Put into Que
			$this -> queue[] = '\'' . $this -> appCore -> db -> strEscape($address) . '\', \'' . $this -> appCore -> db -> strEscape($recipient) . '\', \'' . $this -> appCore -> db -> strEscape($subject) . '\', \'' . $this -> appCore -> db -> strEscape($message) . '\'';
		}
		else
		{
			// Increase number of sent emails
			$this -> sent_emails ++;
			
			// Clear Recipients
			$this -> php_mailer -> ClearAddresses();
			
			// Set New Recipient
			$this -> php_mailer -> AddAddress( $address, $recipient);
			
			// Give Mail to Mailer
			$this -> php_mailer -> Subject 		= $subject;
			$this -> php_mailer -> Body			= $message;
			
			// Append Board's Signature?
			if ( isset( $this -> appCore -> conf['board_email_signature'][0]))
			{
				// Append it
				$this -> php_mailer -> Body .= "\n\n\n-------------------------------------------------------------------------------------------------------\n" . $this -> appCore -> conf['board_email_signature'];
			}
			
			// Send Mail
			$this -> php_mailer -> Send();	
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Save overhead mails in queue
	 *
	 */
	
	function saveQueue(){
		
		// Kill SMTP connection?
		if ( $this -> mass_mailing)
		{
			$this -> php_mailer -> SmtpClose();
		}
		
		// Anything to save in queue?
		if ( isset( $this -> queue[0]))
		{
			// Do Mass-Insert
			$this -> appCore -> db -> multipleInsert( 'mail_to, mail_to_name, mail_subject, mail_text', $this -> queue, 'mail_queue');
					
			// Set Config
			$this -> appCore -> setConfig( 'mails_in_queue', 1);
		}
		
	}
			
}